/***********************************************************
 * Author: 	Brandon McKune
 * E-Mail:	bmckune.cs@gmail.com
 * Date:	September 2, 2011
 * Class:	Advanced OS
 * Time:	1150 - 1325
 * Smstr:	Fall 2011
 */
#include "ClTaskGenerator.h"

ClTaskGenerator* ClTaskGenerator::m_ptrClTaskGenerator = NULL;
uint16_t TaskGenerator_Counter = 0;

/**
 * 	GetNewSRandSeed Function:
 * 	Description:
 * 		This function is used to request a new seed value from
 * 		the randomizer object. This function also seeds SRand
 * 	Parameters:
 * 		N/A
 * 	Return:
 * 		void
 */
void GetNewSRandSeed(){
	//Initialize the psudo random function
	int64_t seed = ClRandomizer::getInstance()->getRandomNum();
	srand(seed);
}

/**
 * 	getInstance() Function:
 * 	Description:
 * 		This method allows the user to get the singleton object
 * 		in order to generate random tasks for testing.
 * 	Parameters:
 * 		N/A
 *	Output:
 *		static ClTaskGenerator ptr to the object controled by the
 *		singleton.
 */
ClTaskGenerator* ClTaskGenerator::getInstance(){
	if(m_ptrClTaskGenerator == NULL){
		m_ptrClTaskGenerator = new ClTaskGenerator();
	}

	return m_ptrClTaskGenerator;
}

/**
 * 	ClTaskGenerator() Function:
 * 	Description:
 * 		This is the default constructor, which was made private in order
 * 		to keep this class a singleton object.  This function does nothing
 * 		at the moment.
 * 	Parameters:
 * 		N/A
 * 	Output:
 * 		N/A
 */
ClTaskGenerator::ClTaskGenerator(){
	GetNewSRandSeed();
}

/**
 * 	~ClTaskGenerator() Destructor:
 * 	Description:
 * 		This method frees up the memory space of the singleton object.
 * 	Parameters:
 * 		N/A
 * 	Output:
 * 		N/A
 */
ClTaskGenerator::~ClTaskGenerator(){
	if(m_ptrClTaskGenerator != NULL){
		delete(m_ptrClTaskGenerator);
		m_ptrClTaskGenerator = NULL;
	}
}

/**
 * 	generateTasks() Function:
 * 	Description:
 * 		This function uses random number generation to determine how many
 * 		tasks must be created.  This ClTask objects that this function
 * 		creates are ready to be used by the algorithm once this function
 * 		returns.
 * 	Parameters:
 * 		uint8_t numTasks:
 * 			This value is pass by reference and represents the max number of
 * 			tasks that should be generated by the generation function.  However,
 * 			because of the nature of this algorithm, the number of tasks is random.
 * 	Output:
 * 		uint8_t numTasks:
 * 			Number of tasks actually generated.  This will be 0 -> numTasks(input).
 * 		vector<ClTask>*:
 * 			The reference to the vector of randomly generated tasks.  Will be numTasks
 * 			in length.  This pointer must be free'd outside by the caller of generateTasks.
 */
vector<ClTask> ClTaskGenerator::generateTasks(const uint16_t numTasks){
	vector<ClTask> vec;
//	if(vec == NULL){
//		cerr << "generateTasks new vector<> failed" << endl;
//		exit(1);
//	}
	vec.clear();

	uint64_t temp_currentTime =
			ClRTClock::getInstance()->getTimeInterval();

	if(TaskGenerator_Counter == 1000){
		GetNewSRandSeed();
		TaskGenerator_Counter = 0;
	}

	//This is to reduce the number of randomly generated tasks.
	//If the time is even, then no tasks be generated.
	if(temp_currentTime % 2 == 0){
//		delete(vec);
		return vec;
	}

	int tasksThisRun =   rand() % numTasks;
	if(tasksThisRun == 0){
		tasksThisRun++;
	}

	for(uint16_t index; index < tasksThisRun; index++){
		ClTask temp;// = new ClTask();
		temp.randomize(g_ProcessorSpeed);
		vec.push_back(temp);
	}

	return vec;
}


